package com.rongcheng.forfeature.dispatcher;

import com.rongcheng.forfeature.actions.Action;
import com.rongcheng.forfeature.stores.Store;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by frc on 16-10-31.
 */

public class Dispatcher {
    private static Dispatcher instance;

    private final List<Store> stores = new ArrayList<>();

    public static Dispatcher get() {
        if (instance == null) {
            instance = new Dispatcher();
        }
        return instance;
    }

    Dispatcher() {
    }

    public void register(final Store store) {
        stores.add(store);
    }

    public void unRegister(final Store store) {
        stores.remove(store);
    }

    public void dispatch(Action action){
        post(action);
    }

    private void post(Action action) {
        for (Store store : stores) {
            store.onAction(action);
        }
    }

}
